﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using GestionPedago.EntityContext;
using GestionPedago.EntityContext.EntityOPTools;
using GestionPedago.Persistence.OPManager;


namespace Persistence.OPManager {
    /// <summary>
    /// Manager de persistance de l'Entity ECF
    /// </summary>
    public abstract class ECFOPManager {

        /// <summary>
        /// Méthode de récupération de la liste des ECF par Module, Competence et libellé
        /// </summary>
        /// <param name="idModule">Identifiant du module</param>
        /// <param name="idCompetence">Identifiant de la compétence</param>
        /// <param name="libelle">Libellé</param>
        /// <returns>La liste des ECF</returns>
        public static List<ECF> GetListeECF(Int32? idModule = null, Int32? idCompetence = null, string libelle = null) {
            List<ECF> listeECF = new List<ECF>();


            try {
                IQueryable<ECF> query = EntityConnection.GetContext().ECF;

                query = query.Where(it => (libelle == null || libelle == string.Empty || it.Libelle.ToUpper().Contains(libelle.ToUpper())));
                query = query.OrderBy(it => it.Libelle);
                /* query.Select(it => it);*/
                ModuleParECF moduleIn = new ModuleParECF();
                if(idModule != null) {
                    moduleIn.IdModule = idModule.Value;
                }
                CompetenceParECF competenceIn = new CompetenceParECF();
                if(idCompetence != null) {
                    competenceIn.IdCompetence = idCompetence.Value;
                }
                bool ajouterALaListe;
                List<ECF> listeECFQuery = query.ToList();
                foreach(ECF e in listeECFQuery) {
                    ajouterALaListe = true;
                    if(idModule != null) {
                        if(!e.ModuleParECF.Contains(moduleIn, new ModuleParECFComparerOnModule())) {
                            ajouterALaListe = false;
                        }
                    }

                    if(idCompetence != null) {
                        if(!e.CompetenceParECF.Contains(competenceIn, new CompetenceParECFComparerOnCompetence())) {
                            ajouterALaListe = false;
                        }
                    }

                    if(ajouterALaListe) {
                        listeECF.Add(e);
                    }
                }


            } catch(Exception e) {
                throw e;
            }

            return listeECF;
        }

        /// <summary>
        /// Méthode de récupération d'un ECF par son identifiant
        /// </summary>
        /// <param name="id">Identifiant de l'ECF à récupérer</param>
        /// <returns>L'ECF</returns>
        public static ECF GetECFById(int id) {

            try {
                var query = from it in EntityConnection.GetContext().ECF
                            where it.IdECF == id
                            select it;

                ECF e = null;

                e = query.First();

                return e;

            } catch(Exception ex) {
                throw ex;
            }

        }

        /// <summary>
        /// Méthode d'ajout d'un ECF dans la base
        /// </summary>
        /// <param name="ecf">L'ECF à ajouter en base</param>
        /// <returns>L'ECF</returns>
        public static ECF AjouterECF(ECF ecf, List<int> listeIdCompetence, List<int> listeIdModule) {

            try {
                List<VersionECF> lstVersion = new List<VersionECF>();
                foreach(VersionECF ve in ecf.VersionECF) {
                    lstVersion.Add(ve);
                }

               
                foreach(VersionECF ve in lstVersion) {
                        EntityConnection.GetContext().VersionECF.Detach(ve);
                }
                

              /*  List<CompetenceParECF> lstCompParECF = new List<CompetenceParECF>();
                foreach(CompetenceParECF cpe in ecf.CompetenceParECF) {
                    lstCompParECF.Add(cpe);
                }

                foreach(CompetenceParECF cpe in lstCompParECF) {
                    EntityConnection.GetContext().CompetenceParECF.Detach(cpe);
                }

                List<ModuleParECF> lstModParECF = new List<ModuleParECF>();
                foreach(ModuleParECF mpe in ecf.ModuleParECF) {
                    lstModParECF.Add(mpe);
                }

                foreach(ModuleParECF mpe in lstModParECF) {
                    EntityConnection.GetContext().ModuleParECF.Detach(mpe);
                }*/

                if(!EntityStateChecker.IsDetached(ecf)) {
                    EntityConnection.GetContext().ECF.Detach(ecf);
                }

                EntityConnection.GetContext().ECF.AddObject(ecf);
                EntityConnection.GetContext().SaveChanges();

               /* foreach(CompetenceParECF cpe in lstCompParECF) {

                    cpe.IdECF = ecf.IdECF;
                    CompetenceParECFOPManager.AjouterCompetenceParECF(cpe);
                }

                foreach(ModuleParECF mpe in lstModParECF) {

                    mpe.IdECF = ecf.IdECF;
                    ModuleParECFOPManager.AjouterModuleParECF(mpe);
                }*/

                foreach(int idComp in listeIdCompetence) {
                    CompetenceParECF cpe = new CompetenceParECF();
                    cpe.IdECF = ecf.IdECF;
                    cpe.IdCompetence = idComp;
                    CompetenceParECFOPManager.AjouterCompetenceParECF(cpe);
                }

               /* foreach(ModuleParECF mpe in lstModParECF) {

                    mpe.IdECF = ecf.IdECF;
                    ModuleParECFOPManager.AjouterModuleParECF(mpe);
                }*/

                foreach(int idMod in listeIdModule) {
                    ModuleParECF mpe = new ModuleParECF();
                    mpe.IdECF = ecf.IdECF;
                    mpe.IdModule = idMod;
                    ModuleParECFOPManager.AjouterModuleParECF(mpe);
                }

                EntityConnection.GetContext().SaveChanges();

                return ecf;

            } catch(Exception ex) {

                throw ex;

            }
        }

        /// <summary>
        /// Méthode de suppression d'un ECF
        /// </summary>
        /// <param name="e">L'ECF à supprimer</param>
        public static void SupprimerECF(ECF e) {
            ECF ec = ECFOPManager.GetECFById(e.IdECF);
            foreach(ModuleParECF mpe in e.ModuleParECF.ToList()) {
                ModuleParECFOPManager.SupprimerModuleParECF(mpe);
            }
            foreach(CompetenceParECF cpe in e.CompetenceParECF.ToList()) {
                CompetenceParECFOPManager.SupprimerCompetenceParECF(cpe);
            }
            foreach(VersionECF ver in e.VersionECF.ToList()) {
                VersionECFOPManager.SupprimerVersionECF(ver);
            }
            EntityConnection.GetContext().ECF.DeleteObject(ec);
            EntityConnection.GetContext().SaveChanges();
        }
    }
}
